<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>2 模块化 | HasakWebBlog</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="hasak">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.a5a0d673.css" as="style"><link rel="preload" href="/assets/js/app.c7915c77.js" as="script"><link rel="preload" href="/assets/js/3.f80332a6.js" as="script"><link rel="preload" href="/assets/js/1.5cffa4a1.js" as="script"><link rel="preload" href="/assets/js/45.8e8ea47a.js" as="script"><link rel="prefetch" href="/assets/js/10.02ae8499.js"><link rel="prefetch" href="/assets/js/11.c5238d8f.js"><link rel="prefetch" href="/assets/js/12.be21fd80.js"><link rel="prefetch" href="/assets/js/13.bd684308.js"><link rel="prefetch" href="/assets/js/14.b5f9fc77.js"><link rel="prefetch" href="/assets/js/15.318683dc.js"><link rel="prefetch" href="/assets/js/16.74f9d94e.js"><link rel="prefetch" href="/assets/js/17.cd1e6d69.js"><link rel="prefetch" href="/assets/js/18.901e5492.js"><link rel="prefetch" href="/assets/js/19.417d2070.js"><link rel="prefetch" href="/assets/js/20.39ec66a2.js"><link rel="prefetch" href="/assets/js/21.fbf1aaf8.js"><link rel="prefetch" href="/assets/js/22.736aeb98.js"><link rel="prefetch" href="/assets/js/23.abcdc310.js"><link rel="prefetch" href="/assets/js/24.26658596.js"><link rel="prefetch" href="/assets/js/25.40bdc691.js"><link rel="prefetch" href="/assets/js/26.29ac009b.js"><link rel="prefetch" href="/assets/js/27.edc6afbf.js"><link rel="prefetch" href="/assets/js/28.b9c1065e.js"><link rel="prefetch" href="/assets/js/29.b46f8f9e.js"><link rel="prefetch" href="/assets/js/30.6839ae7e.js"><link rel="prefetch" href="/assets/js/31.85913f4e.js"><link rel="prefetch" href="/assets/js/32.098b21df.js"><link rel="prefetch" href="/assets/js/33.faced894.js"><link rel="prefetch" href="/assets/js/34.239b19e7.js"><link rel="prefetch" href="/assets/js/35.5c8a8406.js"><link rel="prefetch" href="/assets/js/36.46f0ebac.js"><link rel="prefetch" href="/assets/js/37.2456f410.js"><link rel="prefetch" href="/assets/js/38.b76624ee.js"><link rel="prefetch" href="/assets/js/39.64ecef14.js"><link rel="prefetch" href="/assets/js/4.7d7a493e.js"><link rel="prefetch" href="/assets/js/40.a1304a44.js"><link rel="prefetch" href="/assets/js/41.f98208ce.js"><link rel="prefetch" href="/assets/js/42.f0be4a4c.js"><link rel="prefetch" href="/assets/js/43.1a46f61e.js"><link rel="prefetch" href="/assets/js/44.d579b00f.js"><link rel="prefetch" href="/assets/js/46.1b194721.js"><link rel="prefetch" href="/assets/js/47.994c7d71.js"><link rel="prefetch" href="/assets/js/48.f17a641c.js"><link rel="prefetch" href="/assets/js/49.0d9ea06f.js"><link rel="prefetch" href="/assets/js/5.176296fc.js"><link rel="prefetch" href="/assets/js/50.e36885a9.js"><link rel="prefetch" href="/assets/js/51.64538ee2.js"><link rel="prefetch" href="/assets/js/52.1fd24c2e.js"><link rel="prefetch" href="/assets/js/53.967b1458.js"><link rel="prefetch" href="/assets/js/54.05b15a98.js"><link rel="prefetch" href="/assets/js/55.3c78b9d4.js"><link rel="prefetch" href="/assets/js/56.8d130c30.js"><link rel="prefetch" href="/assets/js/57.0008c022.js"><link rel="prefetch" href="/assets/js/58.4b49a382.js"><link rel="prefetch" href="/assets/js/59.8bb182dc.js"><link rel="prefetch" href="/assets/js/6.90835ce9.js"><link rel="prefetch" href="/assets/js/60.93235335.js"><link rel="prefetch" href="/assets/js/61.78370acd.js"><link rel="prefetch" href="/assets/js/62.1144f314.js"><link rel="prefetch" href="/assets/js/63.7fb72cd2.js"><link rel="prefetch" href="/assets/js/64.de8ba1be.js"><link rel="prefetch" href="/assets/js/65.cefa274d.js"><link rel="prefetch" href="/assets/js/66.32ad44bf.js"><link rel="prefetch" href="/assets/js/67.558c356c.js"><link rel="prefetch" href="/assets/js/68.15a3af19.js"><link rel="prefetch" href="/assets/js/69.b1b26c0a.js"><link rel="prefetch" href="/assets/js/7.2c075495.js"><link rel="prefetch" href="/assets/js/70.cb9b8e94.js"><link rel="prefetch" href="/assets/js/71.e371986a.js"><link rel="prefetch" href="/assets/js/72.a8af9117.js"><link rel="prefetch" href="/assets/js/73.dcc5a1af.js"><link rel="prefetch" href="/assets/js/74.803a909b.js"><link rel="prefetch" href="/assets/js/75.8129f8f6.js"><link rel="prefetch" href="/assets/js/76.ffcb07fc.js"><link rel="prefetch" href="/assets/js/8.9b4cfefe.js"><link rel="prefetch" href="/assets/js/9.e529cf11.js">
    <link rel="stylesheet" href="/assets/css/0.styles.a5a0d673.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-7dd95ae2><div data-v-7dd95ae2><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>HasakWebBlog</h3> <p class="description" data-v-59e6cb88>hasak</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>hasak</span>
          
        <span data-v-59e6cb88>2022 - </span>
        2023
      </a></span></div></div> <div class="hide" data-v-7dd95ae2><header class="navbar" data-v-7dd95ae2><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="HasakWebBlog" class="logo"> <span class="site-name">HasakWebBlog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      前端学习
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/HTML5&amp;CSS3/" class="nav-link"><i class="undefined"></i>
  HTML5&amp;CSS3
</a></li><li class="dropdown-item"><!----> <a href="/categories/Javascript/" class="nav-link"><i class="undefined"></i>
  Javascript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node/" class="nav-link"><i class="undefined"></i>
  Node
</a></li><li class="dropdown-item"><!----> <a href="/categories/VUE/" class="nav-link"><i class="undefined"></i>
  VUE
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tags
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/hasakxu" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-7dd95ae2></div> <aside class="sidebar" data-v-7dd95ae2><div class="personal-info-wrapper" data-v-1fad0c41 data-v-7dd95ae2><img src="/dog.png" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    hasak
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>66</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>5</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      前端学习
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/HTML5&amp;CSS3/" class="nav-link"><i class="undefined"></i>
  HTML5&amp;CSS3
</a></li><li class="dropdown-item"><!----> <a href="/categories/Javascript/" class="nav-link"><i class="undefined"></i>
  Javascript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node/" class="nav-link"><i class="undefined"></i>
  Node
</a></li><li class="dropdown-item"><!----> <a href="/categories/VUE/" class="nav-link"><i class="undefined"></i>
  VUE
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tags
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系我
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/hasakxu" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>2 模块化</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>hasak</span>
          
        <span data-v-59e6cb88>2022 - </span>
        2023
      </a></span></div></div> <div data-v-7dd95ae2><div data-v-7dd95ae2><main class="page" style="padding-right:0;"><section style="display:;"><div class="page-title"><h1 class="title">2 模块化</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>hasak</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>6/26/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>Node</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="_2-模块化"><a href="#_2-模块化" class="header-anchor">#</a> 2 模块化</h1> <h2 id="_2-1-模块化的基本概念"><a href="#_2-1-模块化的基本概念" class="header-anchor">#</a> 2.1 模块化的基本概念</h2> <p>==模块化==：是指解决一个复杂问题时，<strong>自顶向下逐层把系统划分成若干模块的过程</strong>。对于整个系统来说，模块是可组合、分解和更换的单元
编程领域中的模块化，就是<strong>遵守固定的规则，把一个大文件拆成独立并互相依赖的多个小模块。</strong>
把代码进行模块化拆分的好处</p> <ul><li>提高了代码的复用性</li> <li>提高了代码的可维护性</li> <li>可以实现按需加载</li></ul> <p>**模块化规范：**就是对代码进行模块化的拆分与组合时，需要遵守的那些规则。例如使用什么样的语法格式来引用模块，在模块中使用什么样的语法格式向外暴露成员
<strong>模块化规范的好处</strong>：大家都遵守同样的模块化规范写代码，降低了沟通的成本，极大方便了各个模块之间的相互调用，利人利己</p> <h2 id="_2-2-node-js-中的模块化"><a href="#_2-2-node-js-中的模块化" class="header-anchor">#</a> 2.2  Node.js 中的模块化</h2> <h3 id="node-js-中模块的分类"><a href="#node-js-中模块的分类" class="header-anchor">#</a> <strong>Node.js 中模块的分类</strong></h3> <p>Node.js 中根据模块来源的不同，将模块分为了 3 大类</p> <ul><li><p>==内置模块==（内置模块是由 Node.js 官方提供的，例如 fs、path、http 等）</p></li> <li><p>==自定义模块==（用户创建的每个 .js 文件，都是自定义模块）</p></li> <li><p>==第三方模块==（由第三方开发出来的模块，使用前需要先下载）</p></li></ul> <h3 id="加载模块"><a href="#加载模块" class="header-anchor">#</a> 加载模块</h3> <p>使用强大的<code>require()</code>方法，可以加载需要的内置模块、用户自定义模块、第三方模块进行使用</p> <ul><li>注意：使用 require() 方法加载其它模块时，会<strong>执行</strong>被加载模块中的代码</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> fs <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'fs'</span><span class="token punctuation">)</span>							<span class="token comment">// 内置模块</span>
<span class="token keyword">const</span> custom <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./custom.js'</span><span class="token punctuation">)</span>	<span class="token comment">// 自定义模块，需指明路径，可以省略 .js 的后缀名</span>
<span class="token keyword">const</span> moment <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'moment'</span><span class="token punctuation">)</span>			<span class="token comment">// 第三方模块</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="node-js-中的模块作用域"><a href="#node-js-中的模块作用域" class="header-anchor">#</a> Node.js 中的模块作用域</h3> <p><strong>模块作用域</strong>：和函数作用域类似，在自定义模块中定义的变量、方法等成员，只能在当前模块内被访问，这种模块级别的访问限制</p> <ul><li><strong>模块作用域的好处</strong>：防止了全局变量污染的问题</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>
<span class="token comment">// 模块作用域</span>
<span class="token keyword">const</span> username <span class="token operator">=</span> <span class="token string">'张三'</span>

<span class="token keyword">function</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'大家好，我是'</span> <span class="token operator">+</span> username<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> custom <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./模块作用域'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>custom<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="向外共享模块作用域中的成员"><a href="#向外共享模块作用域中的成员" class="header-anchor">#</a> 向外共享模块作用域中的成员</h3> <p><strong>module对象</strong></p> <p>在每个 .js 自定义模块中都有一个<code>module</code>对象，它里面存储了和当前模块有关的信息</p> <p><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211191524404.png" alt="image.png"></p> <p><strong>module.exports 对象</strong></p> <ul><li>在自定义模块中，可以使用<code>module.exports</code>对象，将模块内的成员共享出去，供外界使用</li> <li>外界用 require() 方法导入自定义模块时，得到的就是 <code>module.exports</code>所指向的对象</li></ul> <p><strong>exports 和 <code>module.exports</code>的使用误区</strong></p> <ul><li>由于 <code>module.export</code> 单词写起来比较复杂，为了简化向外共享成员的代码，Node 提供了 <strong>exports 对象</strong>。默认情况下，exports 和 <code>module.exports</code> 指向同一个对象。最终共享的结果，还是以 <strong><code>module.export</code>指向的对象为准</strong></li> <li>时刻谨记，require() 模块时，得到的永远是 <strong><code>module.exports 指向的对象</code></strong></li> <li>注意：为了防止混乱，建议大家不要在同一个模块中同时使用 exports 和 module.exports</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>
<span class="token comment">// 在一个自定义模块中，默认情况下， module.exports = {}</span>

<span class="token keyword">const</span> age <span class="token operator">=</span> <span class="token number">20</span>

<span class="token comment">// 向 module.exports 对象上挂载 username 属性</span>
module<span class="token punctuation">.</span>exports<span class="token punctuation">.</span>username <span class="token operator">=</span> <span class="token string">'zs'</span>
<span class="token comment">// 向 module.exports 对象上挂载 sayHello 方法</span>
module<span class="token punctuation">.</span>exports<span class="token punctuation">.</span><span class="token function-variable function">sayHello</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Hello!'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
module<span class="token punctuation">.</span>exports<span class="token punctuation">.</span>age <span class="token operator">=</span> age

<span class="token comment">// 让 module.exports 指向一个全新的对象</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">nickname</span><span class="token operator">:</span> <span class="token string">'小黑'</span><span class="token punctuation">,</span>
  <span class="token function">sayHi</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Hi!'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 在外界使用 require 导入一个自定义模块的时候，得到的成员，</span>
<span class="token comment">// 就是 那个模块中，通过 module.exports 指向的那个对象</span>
<span class="token keyword">const</span> m <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./11.自定义模块'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>m<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="node-js-中的模块化规范"><a href="#node-js-中的模块化规范" class="header-anchor">#</a> Node.js 中的模块化规范</h3> <p>Node.js 遵循了<code>CommonJS</code> 模块化规范，<code>CommonJS</code>规定了<strong>模块的特性</strong>和各模块之间如何相互依赖</p> <ul><li>每个模块内部，<strong>module 变量</strong>代表当前模块</li> <li>module 变量是一个对象，它的 exports 属性（即 <code>module.exports</code>）<strong>是对外的接口</strong></li> <li>加载某个模块，其实是加载该模块的<code>module.exports</code> 属性。<strong>require()</strong> 方法用于加载模块</li></ul> <h2 id="_2-3-npm与包"><a href="#_2-3-npm与包" class="header-anchor">#</a> 2.3 <code>npm</code>与包</h2> <h3 id="包"><a href="#包" class="header-anchor">#</a> 包</h3> <p>Node.js 中的第三方模块又叫做<strong>包</strong></p> <p>不同于 Node.js 中的内置模块与自定义模块，包是由第三方个人或团队开发出来的，免费供所有人使用。Node.js 中的包都是免费且开源的</p> <ul><li><p>由于 Node.js 的内置模块仅提供了一些底层的 API，导致在基于内置模块进行项目开发的时，效率很低</p></li> <li><p><strong>==包是基于内置模块封装出来的==</strong>，提供了更高级、更方便的 API，极大的提高了开发效率</p></li> <li><p>包和内置模块之间的关系，类似于 jQuery和 浏览器内置 API 之间的关系</p></li></ul> <p>国外有一家 IT 公司，叫做 <strong>npm, Inc.</strong> 这家公司旗下有一个非常著名的网站 https://www.npmjs.com/，它是全球最大的包共享平台，你可以从这个网站上搜索到任何你需要的包，只要你有足够的耐心！到目前位置，全球约1100多万的开发人员，通过这个包共享平台，开发并共享了超过120多万个包 供我们使用。npm, Inc. 公司提供了一个地址为 https://registry.npmjs.org/ 的服务器，来对外共享所有的包，我们可以从这个服务器上下载自己所需要的包。</p> <ol><li><p>从 https://www.npmjs.com/网站上搜索自己所需要的包</p></li> <li><p>从 https://registry.npmjs.org/  服务器上下载自己需要的包</p></li></ol> <p><strong>下载包</strong></p> <p>npm, Inc. 公司提供了一个包管理工具，使用这个工具从 https://registry.npmjs.org/ 服务器把需要的包下载到本地使用。工具的名字叫做 Node Package Manager（简称 npm 包管理工具），这个包管理工具随着 Node.js 的安装包一起被安装到了用户的电脑上。在终端中执行 npm -v命令，来查看自己电脑上所安装的 npm 包管理工具的版本号</p> <h3 id="npm-安装"><a href="#npm-安装" class="header-anchor">#</a> npm 安装</h3> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// dateFormat.js</span>

<span class="token comment">// 1. 定义格式化时间的方法</span>
<span class="token keyword">function</span> <span class="token function">dateFormat</span><span class="token punctuation">(</span><span class="token parameter">dtStr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> dt <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>dtStr<span class="token punctuation">)</span>

  <span class="token keyword">const</span> y <span class="token operator">=</span> dt<span class="token punctuation">.</span><span class="token function">getFullYear</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> m <span class="token operator">=</span> <span class="token function">padZero</span><span class="token punctuation">(</span>dt<span class="token punctuation">.</span><span class="token function">getMonth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> d <span class="token operator">=</span> <span class="token function">padZero</span><span class="token punctuation">(</span>dt<span class="token punctuation">.</span><span class="token function">getDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> hh <span class="token operator">=</span> <span class="token function">padZero</span><span class="token punctuation">(</span>dt<span class="token punctuation">.</span><span class="token function">getHours</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> mm <span class="token operator">=</span> <span class="token function">padZero</span><span class="token punctuation">(</span>dt<span class="token punctuation">.</span><span class="token function">getMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">const</span> ss <span class="token operator">=</span> <span class="token function">padZero</span><span class="token punctuation">(</span>dt<span class="token punctuation">.</span><span class="token function">getSeconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

  <span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>y<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">-</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>m<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">-</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>d<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>hh<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>mm<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">:</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>ss<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span>
<span class="token punctuation">}</span>

<span class="token comment">// 定义补零的函数</span>
<span class="token keyword">function</span> <span class="token function">padZero</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> n <span class="token operator">&gt;</span> <span class="token number">9</span> <span class="token operator">?</span> n <span class="token operator">:</span> <span class="token string">'0'</span> <span class="token operator">+</span> n
<span class="token punctuation">}</span>

module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  dateFormat
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 1. 导入需要的包</span>
<span class="token comment">// 注意：导入的名称，就是装包时候的名称</span>
<span class="token keyword">const</span> moment <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'moment'</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> dt <span class="token operator">=</span> <span class="token function">moment</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">'YYYY-MM-DD HH:mm:ss'</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>dt<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>在项目中安装包的命令</strong></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span> 包的完整名称
或者
<span class="token function">npm</span> i 包的完整名称
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 导入自定义的格式化时间的模块</span>
<span class="token keyword">const</span> <span class="token constant">TIME</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./dateFormat'</span><span class="token punctuation">)</span>

<span class="token comment">// 调用方法，进行时间的格式化</span>
<span class="token keyword">const</span> dt <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// console.log(dt)</span>
<span class="token keyword">const</span> newDT <span class="token operator">=</span> <span class="token constant">TIME</span><span class="token punctuation">.</span><span class="token function">dateFormat</span><span class="token punctuation">(</span>dt<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newDT<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>初次装包完成后，在项目文件夹下多一个叫做 node_modules 的文件夹和 package-lock.json 的配置文件。</p> <ul><li><code>node_modules</code>文件夹用来存放所有已安装到项目中的包。require() 导入第三方包时，从这个目录中查找并加载</li> <li><code>package-lock.json</code> 配置文件用来记录 node_modules 目录下的每一个包的下载信息，例如包的名字、版本号、下载地址等</li></ul> <div class="language-json line-numbers-mode"><pre class="language-json"><code>
<span class="token punctuation">{</span>
  <span class="token property">&quot;requires&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  <span class="token property">&quot;lockfileVersion&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
  <span class="token property">&quot;dependencies&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;moment&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token property">&quot;version&quot;</span><span class="token operator">:</span> <span class="token string">&quot;2.29.1&quot;</span><span class="token punctuation">,</span>
      <span class="token property">&quot;resolved&quot;</span><span class="token operator">:</span> <span class="token string">&quot;https://registry.npmjs.org/moment/-/moment-2.29.1.tgz&quot;</span><span class="token punctuation">,</span>
      <span class="token property">&quot;integrity&quot;</span><span class="token operator">:</span> <span class="token string">&quot;sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==&quot;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>**注意：**不要手动修改 node_modules 或 package-lock.json文件中的任何代码，npm 包管理工具会自动维护它们</p> <p><strong>安装指定版本的包</strong>
npm install 命令默认安装最新版本的包。如需安装指定版本的包，在包名之后，@版本号</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> i 包的完整名称@版本号
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>包的版本号以“点分十进制”形式进行定义，总共有三位数字，如 2.24.0。其中每一位数字所代表的的含义如下
第1位数字：大版本
第2位数字：功能版本
第3位数字：Bug修复版本
版本号提升的规则：只要前面的版本号增长了，则后面的版本号归零</p> <h3 id="包管理配置文件"><a href="#包管理配置文件" class="header-anchor">#</a> 包管理配置文件</h3> <p>npm规定，在项目根目录中，必须提供一个叫做 package.json 的包管理配置文件。用来记录与项目有关的一些配置信息。如</p> <ul><li>项目的名称、版本号、描述等</li> <li>项目中都用到了哪些包</li> <li>哪些包只在开发期间会用到</li> <li>那些包在开发和部署时都需要用到</li></ul> <ol><li>多人协作的问题
遇到的问题：第三方包的体积过大，不方便团队成员之间共享项目源代码。
解决方案：共享时剔除 node_modules</li> <li>如何记录项目中安装了哪些包
在项目根目录 <code>package.json</code> 的配置文件用来记录项目中安装了哪些包。从而方便剔除 node_modules 目录之后，在团队成员之间共享项目的源代码
注意：今后在项目开发中，一定要把 node_modules文件夹，添加到 <code>.gitignore</code>忽略文件中</li> <li>快速创建<code>package.json</code> <code>npm</code> 包管理工具提供了一个快捷命令，可以在执行命令时所处的目录中，快速创建 <code>package.json</code>这个包管理配置文件</li></ol> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> init <span class="token parameter variable">-y</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>上述命令只能在英文的目录下成功运行！所以项目文件夹的名称一定要使用英文命名，不能出现空格</li> <li><strong>运行 npm install 命令安装包的时候，npm包管理工具会自动把包的名称和版本号，记录到 package.json</strong> <ul><li><code>package.json</code> 文件中的 <strong>dependencies</strong> 节点，专门用来记录您使用 npm install 命令安装了哪些包</li></ul></li></ul> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
  <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;test&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;version&quot;</span><span class="token operator">:</span> <span class="token string">&quot;1.0.0&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;description&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;main&quot;</span><span class="token operator">:</span> <span class="token string">&quot;index.js&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;scripts&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;test&quot;</span><span class="token operator">:</span> <span class="token string">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token property">&quot;keywords&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token property">&quot;author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;license&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ISC&quot;</span><span class="token punctuation">,</span>
  <span class="token property">&quot;dependencies&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;art-template&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^4.13.2&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;jquery&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^3.6.0&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;moment&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^2.29.1&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p><strong>一次性安装所有的包</strong></p> <ul><li>拿到一个剔除了 node_modules 的项目之后，需要先把所有的包下载到项目中，才能将项目运行起来。否则会报类似于下面的错误</li></ul> <p><img src="https://hasakwebblogimg.oss-cn-hangzhou.aliyuncs.com/202211191530835.png" alt="image.png"></p> <ul><li>可以运行 npm install 命令（或 npm i）一次性安装所有的依赖包</li></ul> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>卸载包</strong></p> <ul><li>运行 npm uninstall 命令，来卸载指定的包</li></ul> <div class="language- line-numbers-mode"><pre class="language-text"><code>npm uninstall 包名
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>npm uninstall 命令执行成功后，会把卸载的包，自动从 package.json 的 dependencies 中移除掉</li></ul> <p><strong>devDependencies节</strong></p> <ul><li>如果某些包只在项目开发阶段会用到，在项目上线之后不会用到，则建议把这些包记录到 <strong>devDependencies</strong>节点中。如果某些包在开发和项目上线之后都需要用到，则建议把这些包记录到 <strong>dependencies</strong> 节点中。可以使用如下的命令，将包记录到 devDependencies节点中</li></ul> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span> 包名 --save-dev
或
<span class="token function">npm</span> i 包名 <span class="token parameter variable">-D</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>解决下包速度慢的问题</strong></p> <ul><li><p>npm 下包的时候，默认从国外的 https://registry.npmjs.org/ 服务器进行下载，可能会慢</p></li> <li><p>使用国内镜像服务器-淘宝，大幅改善下载速度</p></li></ul> <p><strong>切换npm 的下包镜像源</strong></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> config get registry 	<span class="token comment"># 查看当前包镜像源</span>
<span class="token function">npm</span> config <span class="token builtin class-name">set</span> <span class="token assign-left variable">registry</span><span class="token operator">=</span>http://registry.npm.taobao.org/ <span class="token comment"># 切换源</span>
<span class="token function">npm</span> config get registry 	<span class="token comment"># 检查镜像源是否下载成功</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>为了方便的切换下包的镜像源，安装nrm小工具，利用 nrm 提供的终端命令，快速查看和切换下包的镜像源</li></ul> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> <span class="token function">install</span> nrm <span class="token parameter variable">-g</span>	<span class="token comment"># -g 全局</span>
nrm <span class="token function">ls</span>							<span class="token comment"># 查看当前可用的镜像源地址</span>
nrm use taobao			<span class="token comment"># 切换镜像源</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>包的分类</strong></p> <p>使用 npm 包管理工具下载的包，共分为两大类，分别是
<strong>1. 项目包</strong>
那些被安装到项目的 node_modules 目录中的包，都是项目包。项目包又分为两类</p> <ul><li>开发依赖包（被记录到 devDependencies节点中的包，只在开发期间会用到）<code>npm i 包名 -D</code></li> <li>核心依赖包（被记录到 dependencies节点中的包，在开发期间和项目上线之后都会用到）<code>npm i 包名</code></li></ul> <p><strong>2. 全局包</strong>
在执行 npm install 命令时，如果提供了 -g 参数，则会把包安装为全局包
全局包会被安装到 C:\Users\用户目录\AppData\Roaming\npm\node_modules 目录下
<strong>注意</strong></p> <ul><li><strong>只有工具性质的包，才有全局安装的必要性</strong>。因为它们提供了好用的终端命令</li> <li>判断某个包是否需要全局安装后才能使用，可以参考<strong>官方提供的使用说明</strong>即可</li></ul> <p><strong>i5ting_toc</strong></p> <ul><li>i5ting_toc 是一个可以把 md 文档转为 html 页面的小工具</li></ul> <div class="language-SHELL line-numbers-mode"><pre class="language-shell"><code><span class="token function">npm</span> i i5ting_toc <span class="token parameter variable">-g</span>						<span class="token comment"># 安装</span>
i5ting_toc <span class="token parameter variable">-f</span> sample.md <span class="token parameter variable">-o</span>		<span class="token comment"># 转换</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>规范的包结构</strong></p> <ul><li>一个规范的包，它的组成结构，必须符合以下 3 点要求
<ul><li>包必须以单独的目录而存在</li> <li>包的顶级目录下要必须包含 package.json 这个包管理配置文件</li> <li><code>package.json</code> 中必须包含 <strong>name，version，main这三个属性</strong>，分别代表包的名字、版本号、包的入口（require()加载的文件）</li></ul></li></ul> <p>●关于更多的约束参考 https://yarnpkg.com/zh-Hans/docs/package-json</p> <p><strong>开发包</strong></p> <ol><li><p>新建<code>itheima-tools</code>文件夹，作为包的根目录</p></li> <li><p>在 <code>itheima-tools</code> 文件夹中，新建</p> <ul><li><p><code>package.json</code> （包管理配置文件）<code>npm init -y</code></p></li> <li><p>index.js          （包的入口文件）</p></li></ul></li></ol> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// src 文件夹下开发代码，导入到 index.js 中</span>
<span class="token keyword">const</span> date <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./src/dateFormat'</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> escape <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./src/htmlEscape'</span><span class="token punctuation">)</span>

<span class="token comment">// 向外暴露需要的成员</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token operator">...</span>date<span class="token punctuation">,</span>		<span class="token comment">// ... 展开运算符，将data所有属性交给新对象</span>
  <span class="token operator">...</span>escape		<span class="token comment">// ... 展开运算符，将escape所有属性交给新对象</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li>src 源代码</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// dateFormat.js</span>

<span class="token keyword">function</span> <span class="token function">dateFormat</span><span class="token punctuation">(</span><span class="token parameter">dateStr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">/* 略 */</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">padZero</span><span class="token punctuation">(</span><span class="token parameter">n</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> n <span class="token operator">&gt;</span> <span class="token number">9</span> <span class="token operator">?</span> n <span class="token operator">:</span> <span class="token string">'0'</span> <span class="token operator">+</span> n<span class="token punctuation">}</span>

module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  dateFormat
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>
<span class="token comment">// htmlEscape.js</span>

<span class="token keyword">function</span> <span class="token function">htmlEscape</span><span class="token punctuation">(</span><span class="token parameter">htmlstr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">/* 略 */</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">htmlUnEscape</span><span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">/* 略 */</span><span class="token punctuation">}</span>

module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  htmlEscape<span class="token punctuation">,</span>
  htmlUnEscape
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><ul><li>README.md  （包的说明文档）</li></ul> <div class="language-markdown line-numbers-mode"><pre class="language-markdown"><code><span class="token title important"><span class="token punctuation">##</span> 安装</span>
<span class="token code"><span class="token punctuation">```</span>
<span class="token code-block">npm install itheima-tools</span>
<span class="token punctuation">```</span></span>

<span class="token title important"><span class="token punctuation">##</span> 导入</span>
<span class="token code"><span class="token punctuation">```</span><span class="token code-language">js</span>
<span class="token code-block language-js"><span class="token keyword">const</span> itheima <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'itheima-tools'</span><span class="token punctuation">)</span></span>
<span class="token punctuation">```</span></span>

<span class="token title important"><span class="token punctuation">##</span> 格式化时间</span>
<span class="token code"><span class="token punctuation">```</span><span class="token code-language">js</span>
<span class="token code-block language-js"><span class="token comment">// 调用 dateFormat 对时间进行格式化</span>
<span class="token keyword">const</span> dtStr <span class="token operator">=</span> itheima<span class="token punctuation">.</span><span class="token function">dateFormat</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 结果  2020-04-03 17:20:58</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>dtStr<span class="token punctuation">)</span></span>
<span class="token punctuation">```</span></span>

<span class="token title important"><span class="token punctuation">##</span> 转义 HTML 中的特殊字符</span>
<span class="token code"><span class="token punctuation">```</span><span class="token code-language">js</span>
<span class="token code-block language-js">略</span>
<span class="token punctuation">```</span></span>

<span class="token title important"><span class="token punctuation">##</span> 还原 HTML 中的特殊字符</span>
<span class="token code"><span class="token punctuation">```</span><span class="token code-language">js</span>
<span class="token code-block language-js">略</span>
<span class="token punctuation">```</span></span>

<span class="token title important"><span class="token punctuation">##</span> 开源协议</span>
ISC
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><ul><li>使用</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> itheima <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./itheima-tools'</span><span class="token punctuation">)</span>

<span class="token comment">// 格式化时间的功能</span>
<span class="token keyword">const</span> dtStr <span class="token operator">=</span> itheima<span class="token punctuation">.</span><span class="token function">dateFormat</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>dtStr<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'-----------'</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> htmlStr <span class="token operator">=</span> <span class="token string">'&lt;h1 title=&quot;abc&quot;&gt;这是h1标签&lt;span&gt;123&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;'</span>
<span class="token keyword">const</span> str <span class="token operator">=</span> itheima<span class="token punctuation">.</span><span class="token function">htmlEscape</span><span class="token punctuation">(</span>htmlStr<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'-----------'</span><span class="token punctuation">)</span>

<span class="token keyword">const</span> str2 <span class="token operator">=</span> itheima<span class="token punctuation">.</span><span class="token function">htmlUnEscape</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p><strong>发布包</strong></p> <ol><li><p>https://www.npmjs.com/ 注册 npm 账号</p></li> <li><p>终端中执行<code>npm login</code> 命令，依次输入用户名、密码、邮箱后，即可登录成功</p> <ul><li>注意：执行命令前，必须先把下包的服务器地址切换为 npm 的官方服务器。否则会导致发布包失败！</li></ul></li> <li><p>终端切换到包的根目录之后，运行 <code>npm publish</code>命令，即可将包发布到 npm 上（注意：包名不能雷同）</p></li> <li><p>运行 <code>npm unpublish 包名</code> --force命令，即可从 npm 删除已发布的包</p> <ul><li><p>npm unpublish 命令只能删除 72 小时以内发布的包</p></li> <li><p>npm unpublish 删除的包，在 24 小时内不允许重复发布</p></li> <li><p>发布包的时候要慎重，尽量不要往 npm 上发布没有意义的包</p></li></ul></li></ol> <h2 id="_2-4-模块的加载机制"><a href="#_2-4-模块的加载机制" class="header-anchor">#</a> 2.4 模块的加载机制</h2> <p>==模块在第一次加载后会被缓存==，多次调用 require() 模块的代码只会被执行一次。不论是内置模块、用户自定义模块、还是第三方模块，它们都会优先从缓存中加载，从而提高模块的加载效率。</p> <p><strong>内置模块的加载机制</strong></p> <ul><li><strong>==内置模块的加载优先级最高==</strong></li></ul> <p><strong>自定义模块的加载机制</strong></p> <ul><li><p>使用 require() 加载自定义模块时，<strong>必须指定以 ./ 或 ../ 开头的路径标识符</strong>。在加载自定义模块时，如果没有指定 ./ 或 ../ 这样的路径标识符，则 node 会把它当作内置模块或第三方模块进行加载。</p></li> <li><p>在使用 require() 导入自定义模块时，如果省略了文件的扩展名，Node.js 会按顺序分别尝试加载以下的文件</p> <ul><li>按照确切的文件名进行加载</li> <li>补全 .js 扩展名进行加载</li> <li>补全 .json 扩展名进行加载</li> <li>补全 .node 扩展名进行加载</li> <li>加载失败，终端报错</li></ul></li></ul> <p><strong>第三方模块的加载机制</strong></p> <ul><li><p>如果传递给 require() 的模块标识符不是一个内置模块，也没有以 ./ 或 ../ 开头，则 Node.js 会从当前模块的父目录开始，尝试从 /node_modules 文件夹中加载第三方模块</p></li> <li><p>如果没有找到对应的第三方模块，则移动到再上一层父目录中，进行加载，直到文件系统的根目录</p></li> <li><p>假设在 'C:\Users\itheima\project\node_modules\a.js' 里调用 require('tools')，Node.js 会按以下顺序查找</p> <ul><li>C:\Users\itheima\project\node_modules\tools</li> <li>C:\Users\itheima\node_modules\tools</li> <li>C:\Users\node_modules\tools</li> <li>C:\node_modules\tools</li> <li>报错</li></ul></li></ul> <p><strong>目录作为模块</strong></p> <ul><li>当把目录作为模块标识符，传递给 require() 进行加载的时候，有三种加载方式
<ul><li>在被加载的目录下查找一个叫做 package.json 的文件，并寻找 main 属性值作为 require() 加载的入口</li> <li>如果目录里没有 package.json 文件，或者 main 入口不存在或无法解析，则 Node.js 将会试图加载目录下的 index.js 文件</li> <li>如果以上两步都失败了，则 Node.js 会在终端打印错误消息，报告模块的缺失：Error: Cannot find module 'xxx'</li></ul></li></ul></div></section> <footer class="page-edit"><!----> <!----></footer> <!----> <div class="comments-wrapper"><!----></div></main></div> <!----></div> <ul class="sub-sidebar sub-sidebar-wrapper" style="width:0;" data-v-b57cc07c data-v-7dd95ae2></ul></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/assets/js/app.c7915c77.js" defer></script><script src="/assets/js/3.f80332a6.js" defer></script><script src="/assets/js/1.5cffa4a1.js" defer></script><script src="/assets/js/45.8e8ea47a.js" defer></script>
  </body>
</html>
